package avicit.im.lantop.basic.libstructuredefine.action.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import avicit.im.lantop.basic.libstructuredefine.action.service.LantopArchiveClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import avicit.cbb.appreg.service.CommSysInfoIntfsService;
import avicit.im.lantop.basic.libstructuredefine.domain.LantopArchiveClassT;
import avicit.im.lantop.basic.libstructuredefine.domain.LantopArchiveTabClassV;
import avicit.platform6.commons.utils.ComUtil;
import avicit.platform6.commons.utils.PojoUtil;
import avicit.platform6.commons.utils.web.TreeNode;
import avicit.platform6.core.domain.BeanBase;
import avicit.platform6.core.exception.DaoException;
import avicit.platform6.core.jdbc.JdbcAvicit;
import avicit.platform6.core.properties.PlatformConstant.OpResult;
import avicit.platform6.core.properties.PlatformConstant.OpType;
import avicit.platform6.core.service.ServiceBase;
import avicit.platform6.modules.system.syslog.service.LogBaseFactory;
import avicit.platform6.modules.system.syslog.service.SysLogUtil;

/**
 * <p>金航数码科技有限责任公司</p>
 * <p>作者：钟一华</p>
 * <p>邮箱：zhongyih@avicit.com</p>
 * <p>创建时间： 2015-11-3 下午2:13:16 </p>
 * <p>类说明：库结构定义模板树接口实现类</p>
 * <p>修改记录： </p>
 */
@Service
@SuppressWarnings("unchecked")
public class LantopArchiveClassServiceImpl extends ServiceBase implements LantopArchiveClassService {

	@Resource
	JdbcTemplate jdbcTemplate;

	@Autowired(required = false)
	private JdbcAvicit jdbcAvicit;

	CommSysInfoIntfsService commSysInfoIntfsService;

	@Override
	public LantopArchiveClassT getLantopArchiveClassById(String id) throws  Exception{
		LantopArchiveClassT h = (LantopArchiveClassT) hibernateDao.getSession().get(LantopArchiveClassT.class, id);
		return h;
	}

	@Override
	public void insertLantopArchiveClass(LantopArchiveClassT lantopArchiveClass) throws DaoException {
		String id = ComUtil.getId();
		lantopArchiveClass.setId(id);
		this.insert(lantopArchiveClass);
	}

	@Override
	public void updateLantopArchiveClass(LantopArchiveClassT lantopArchiveClass) throws Exception {
		this.update(lantopArchiveClass);
	}

	@Override
	public void deleteLantopArchiveClassById(String id) throws Exception {
		LantopArchiveClassT lantopArchiveClass = this.getLantopArchiveClassById(id);
		if (lantopArchiveClass == null) {
			throw new DaoException("LantopArchiveClass对象不存在！");
		}
		hibernateDao.delete(lantopArchiveClass);
		SysLogUtil.log(lantopArchiveClass, null, OpType.delete, OpResult.success);
	}

	@Override
	public void insert(BeanBase bean) throws DaoException {
		LantopArchiveClassT lantopArchiveClass = (LantopArchiveClassT) bean;
		if (lantopArchiveClass == null) {
			throw new DaoException(bean.toString() + "对象转换为LantopArchiveClass对象出错！");
		}
		lantopArchiveClass.setId(ComUtil.getId());
		PojoUtil.setSysProperties(lantopArchiveClass, OpType.insert);
		hibernateDao.save(lantopArchiveClass);
		SysLogUtil.log(lantopArchiveClass, null, OpType.insert, OpResult.success);
	}

	@Override
	public void update(BeanBase bean) throws DaoException {
		LantopArchiveClassT lantopArchiveClass = (LantopArchiveClassT) bean;
		if (lantopArchiveClass == null) {
			throw new DaoException(bean.toString() + "对象转换为LantopArchiveClass对象出错！");
		}
		LantopArchiveClassT oldBean = getLantopArchiveClassById(lantopArchiveClass.getId());
		this.hibernateDao.evict(oldBean);
		LantopArchiveClassT newBean = new LantopArchiveClassT();
		PojoUtil.copyProperties(newBean, oldBean, true);
		PojoUtil.copyProperties(newBean, lantopArchiveClass, true);
		SysLogUtil.log(newBean, oldBean, OpType.update, OpResult.success);
		PojoUtil.setSysProperties(newBean, OpType.update);
		this.hibernateDao.update(newBean);
	}

	@Override
	public void delete(BeanBase bean) throws DaoException {
		LantopArchiveClassT lantopArchiveClass = (LantopArchiveClassT) bean;
		if (lantopArchiveClass == null) {
			throw new DaoException(bean.toString() + "对象转换为LantopArchiveClass对象出错！");
		}
		hibernateDao.delete(lantopArchiveClass);
		SysLogUtil.log(lantopArchiveClass, null, OpType.delete, OpResult.success);
	}

	@Override
	public void deleteLantopArchiveClassBatch(String ids) {
		Assert.notNull(ids);
		String[] idArray = ids.split(",");
		for (int i = 0; i < idArray.length; i++) {
			this.deleteLantopArchiveClassById(idArray[i]);
		}
	}

	@Override
	public LantopArchiveTabClassV getLantopArchiveTabClassVById(String id) {
		LantopArchiveTabClassV h = (LantopArchiveTabClassV) hibernateDao.getSession().get(LantopArchiveTabClassV.class, id);
		return h;
	}

	// ////////////////////////////////////////

	/**
	 * 判断是否存在根节点
	 * 
	 * @return
	 */
	public int getParentNodeByParentId(String deptId, String orgId) throws Exception{
		// 获取当前人对应的组织Id
		/*
		 * StringBuffer sb1 = new StringBuffer(); sb1.append("select t.org_id as orgId from sys_dept t where t.id='" + deptId + "'"); List<Map<String, Object>> list1 =
		 * jdbcTemplate.queryForList(sb1.toString()); String orgId = list1.get(0).get("orgId") + "";
		 */

		StringBuffer sb2 = new StringBuffer();
		sb2.append("select count(*) as count from LANTOP_ARCHIVE_CLASS t \n");
		sb2.append(" where t.ORG_ID='" + orgId + "' and t.PARENT_ID='ROOT'");
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sb2.toString());
		Map<String, Object> map = list.get(0);
		int returnCount = 0;
		returnCount = Integer.parseInt(map.get("count") + "");

		return returnCount;

	}

	/**
	 * 根据部门id获取组织id
	 * 
	 * @param deptId
	 * @return
	 */
	@Override
	public String getOrgIdByDeptId(String deptId) throws Exception{
		// 获取当前人对应的组织Id
		StringBuffer sb = new StringBuffer();
		sb.append("select t.org_id as orgId from sys_dept t where t.id='" + deptId + "'");
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sb.toString());
		Map<String, Object> map = list.get(0);
		String orgId = map.get("orgId") + "";
		return orgId;
	}

	/**
	 * 根据当前人对应组织构建根节点
	 * 
	 * @param deptId
	 * @return
	 * @throws Exception
	 */
	public LantopArchiveClassT saveRootLantopArchiveTabClassVByType(String deptId, String orgId) throws Exception {
		// 获取当前人对应的组织Id
		/*
		 * StringBuffer sb1 = new StringBuffer(); sb1.append("select t.org_id as orgId  from sys_dept t where t.id='" + deptId + "'"); List<Map<String, Object>> list1 =
		 * jdbcTemplate.queryForList(sb1.toString()); String orgId = list1.get(0).get("orgId") + "";
		 */

		if (null == deptId || deptId.equals("")) {
			deptId = "1";
		}

		StringBuffer sb2 = new StringBuffer();
		sb2.append("select t.id,t.org_code as orgCode ,t.org_name as orgName from bpm_def_org_v t where t.id='" + orgId + "'");
		List<Map<String, Object>> list2 = jdbcTemplate.queryForList(sb2.toString());
		Map<String, Object> map = list2.get(0);
		LantopArchiveClassT lantopArchiveClass = new LantopArchiveClassT();
		String uuId = ComUtil.getId();
		lantopArchiveClass.setId(uuId);
		lantopArchiveClass.setOrgId(orgId);
		lantopArchiveClass.setDeptId(deptId);
		lantopArchiveClass.setSysId("LANTOP");
		lantopArchiveClass.setArchiveClassNodeCode(map.get("orgCode") + "");
		lantopArchiveClass.setArchiveClassNodeName(map.get("orgName") + "");
		lantopArchiveClass.setParentId("ROOT");
		lantopArchiveClass.setArchiveClassNodePath(uuId);
		// 添加 2015-1-7
		PojoUtil.setSysProperties(lantopArchiveClass, OpType.insert);
		this.hibernateDao.save(lantopArchiveClass);
		// insertLantopArchiveClass(lantopArchiveClass);
		SysLogUtil.log(lantopArchiveClass, null, OpType.insert, OpResult.success);
		return lantopArchiveClass;
	}

	/**
	 * 递归获取Tree当前节点下数据
	 * 
	 * @param id
	 *            当前节点
	 * @param level
	 *            需要获取的层数
	 * @return 节点集合
	 */
	public List<TreeNode> recurFindTreeNodesByParentId(String id, int level, String orgId) {

		// 递归一层减一层
		--level;

		List<TreeNode> nodes = new ArrayList<TreeNode>();

		// 查询业务数据
		List<Map<String, Object>> childTreeList = findNodesByParentId(id, orgId);

		// 将业务数据格式转换成esayui树形格式
		for (Map<String, Object> row : childTreeList) {
			String rid = (String) row.get("ID");
			String rname = (String) row.get("ARCHIVE_CLASS_NODE_NAME");
			String nodeType = (String) row.get("ARCHIVE_CLASS_NODE_TYPE");
			String archiveType = (String) row.get("ARCHIVE_TYPE");
			String parentId = (String) row.get("PARENT_ID");
			TreeNode node = new TreeNode();
			node.setId(rid);
			node.setText(rname);
			// 根据节点类型,设置节点图标
			if (null != parentId && "ROOT".equals(parentId)) { // 根节点
				node.setIconCls("icon-im-newnorg");
			} else if (null != archiveType && "-1".equals(archiveType)) { // -1 档案类型 物理表
				node.setIconCls("icon-im-newntable");
			} else if (null != archiveType && "0".equals(archiveType)) { // 0模版文件夹
				node.setIconCls("icon-im-newnmodulefile");
			} else if (null != archiveType && "1".equals(archiveType)) { // 1标识文件
				node.setIconCls("icon-im-newnmodulelib");
			} else if (null != archiveType && "2".equals(archiveType)) { // 2 档案
				node.setIconCls("icon-im-newnmodulelib");
			} else if (null != archiveType && "3".equals(archiveType)) { // 3 专题库
				node.setIconCls("icon-im-newnmodulelib");
			} else if (null != archiveType && "4".equals(archiveType)) { // 4 多媒体
				node.setIconCls("icon-im-newnmodulelib");
			} else if (null != archiveType && "5".equals(archiveType)) { // 5 项目归档计划
				node.setIconCls("icon-im-newnmodulelib");
			} else if (null != archiveType && "6".equals(archiveType)) { // 6 管务
				node.setIconCls("icon-im-newnmodulelib");
			} else if (null != archiveType && "7".equals(archiveType)) { // 7 其他
				node.setIconCls("icon-im-newnmodulelib");
			} else if (null != archiveType && "8".equals(archiveType)) { // 8 资料
				node.setIconCls("icon-im-newnmodulelib");
			} else if (null != archiveType && "9".equals(archiveType)) { // 9 图书
				node.setIconCls("icon-im-newnmodulelib");
			} else if (null != archiveType && "10".equals(archiveType)) { // 10
				node.setIconCls("icon-im-newnmodulelib");
			} else if (null != nodeType && "1".equals(nodeType)) { // 节点类型 1 文件夹
				node.setIconCls("icon-im-newnmodulefile");
			} else if (null != nodeType && "2".equals(nodeType)) { // 数据节点
				node.setIconCls("icon-im-newntable");
			} else if (null != archiveType && "TECH".equals(archiveType)) { // 10
				node.setIconCls("icon-im-newnmodulelib");
			}
			node.setAttributes(row);
			node.set_parentId((String) row.get("PARENT_ID"));

			BigDecimal childCount2 = BigDecimal.valueOf(Long.valueOf(row.get("CHILD_COUNT") + ""));
			if (null != childCount2 && childCount2.longValue() > 0) {
				if (level > 0) {
					List<TreeNode> cnodes = recurFindTreeNodesByParentId(rid, level, orgId);
					node.getChildren().addAll(cnodes);
					node.setState("open");
				} else {
					node.setState("closed");
				}
			} else {
				node.setState("open");
			}
			nodes.add(node);
		}
		return nodes;
	}

	/**
	 * 获取子节点
	 * 
	 * @param parentId
	 * @return
	 */
	public List<Map<String, Object>> findNodesByParentId(String parentId, String orgId) {
		Assert.notNull(parentId, "参数不能为空！");
		StringBuffer sbSql = new StringBuffer();
		sbSql.append("SELECT (SELECT COUNT(*)\n");
		sbSql.append("          FROM LANTOP_ARCHIVE_TAB_CLASS_V D2\n");
		sbSql.append("         WHERE D2.PARENT_ID = D1.ID) CHILD_COUNT,\n");
		sbSql.append("       D1.*\n");
		sbSql.append("  FROM LANTOP_ARCHIVE_TAB_CLASS_V D1\n");
		sbSql.append(" WHERE D1.PARENT_ID = ? AND ORG_ID = ? \n");
		sbSql.append(" ORDER BY D1.NODE_SORT_NUMBER ASC,D1.CREATION_DATE ASC");
		return this.jdbcTemplate.queryForList(sbSql.toString(), parentId, orgId);
	}

	public List<Map<String, Object>> getPublicFieldInfo() throws Exception{
		StringBuffer sbSql = new StringBuffer();
		sbSql.append("WITH T1 AS\n");
		sbSql.append(" (SELECT\n");
		sbSql.append("   LAC.ID,\n");
		sbSql.append("   LAC.ARCHIVE_CLASS_NODE_CODE AS ARCHIVE_CLASS_NODE_CODE,\n");
		sbSql.append("   LAC.ARCHIVE_CLASS_NODE_NAME AS ARCHIVE_CLASS_NODE_NAME,\n");
		sbSql.append("   '-1' AS TABLE_NAME,\n");
		sbSql.append("   LAC.PARENT_ID,\n");
		sbSql.append("   LAC.ARCHIVE_TYPE,\n");
		sbSql.append("   LAC.ARCHIVE_CLASS_NODE_TYPE,\n");
		sbSql.append("   LAC.IS_VISIBLE,\n");
		sbSql.append("   LAC.NODE_SORT_NUMBER,\n");
		sbSql.append("   LAC.RELAT_WORKFLOW,\n");
		sbSql.append("   LAC.TABLE_LAYER,\n");
		sbSql.append("   LAC.ARCHIVE_CLASS_NODE_PATH,\n");
		sbSql.append("   LAC.REMARK,\n");
		sbSql.append("   LAC.ORG_ID,\n");
		sbSql.append("   LAC.DEPT_ID,\n");
		sbSql.append("   LAC.SYS_ID,\n");
		sbSql.append("   LAC.SECRET_LEVEL,\n");
		sbSql.append("   LAC.SYS_APPLICATION_ID,\n");
		sbSql.append("   LAC.CREATED_BY,\n");
		sbSql.append("   LAC.CREATION_DATE,\n");
		sbSql.append("   LAC.LAST_UPDATED_BY,\n");
		sbSql.append("   LAC.LAST_UPDATE_DATE,\n");
		sbSql.append("   LAC.LAST_UPDATE_IP,\n");
		sbSql.append("   LAC.VERSION,\n");
		sbSql.append("   LAC.ATTRIBUTE_01,\n");
		sbSql.append("   LAC.ATTRIBUTE_02,\n");
		sbSql.append("   LAC.ATTRIBUTE_03,\n");
		sbSql.append("   LAC.ATTRIBUTE_04,\n");
		sbSql.append("   LAC.ATTRIBUTE_05,\n");
		sbSql.append("   LAC.ATTRIBUTE_06,\n");
		sbSql.append("   LAC.ATTRIBUTE_07,\n");
		sbSql.append("   LAC.ATTRIBUTE_08,\n");
		sbSql.append("   LAC.ATTRIBUTE_09,\n");
		sbSql.append("   LAC.ATTRIBUTE_10,\n");
		sbSql.append("   LAC.ATTRIBUTE_11,\n");
		sbSql.append("   LAC.ATTRIBUTE_12,\n");
		sbSql.append("   LAC.ATTRIBUTE_13,\n");
		sbSql.append("   LAC.ATTRIBUTE_14,\n");
		sbSql.append("   LAC.ATTRIBUTE_15,\n");
		sbSql.append("   -1 AS TAB_ORDER,\n");
		sbSql.append("   0 AS MAP_SET_TGT_COUNT,\n");
		sbSql.append("   0 AS MAP_SET_TGT_PUB_COUNT,\n");
		sbSql.append("   '' AS IS_TRUE_TABLE\n");
		sbSql.append("    FROM LANTOP_ARCHIVE_CLASS LAC\n");
		sbSql.append("   WHERE LAC.ARCHIVE_CLASS_NODE_TYPE = ''\n");
		sbSql.append("      OR LAC.ARCHIVE_CLASS_NODE_TYPE IS NULL),\n");
		sbSql.append("T2 AS\n");
		sbSql.append(" (SELECT *\n");
		sbSql.append("    FROM T1\n");
		sbSql.append("  UNION ALL\n");
		sbSql.append("  SELECT CT.ID,\n");
		sbSql.append("         T1.ARCHIVE_CLASS_NODE_CODE AS ARCHIVE_CLASS_NODE_CODE,\n");
		sbSql.append("         CT.TABLE_TITLE AS ARCHIVE_CLASS_NODE_NAME,\n");
		sbSql.append("         CT.TABLE_NAME AS TABLE_NAME,\n");
		sbSql.append("         T1.ID AS PARENT_ID,\n");
		sbSql.append("         '-1' AS ARCHIVE_TYPE,\n");
		sbSql.append("         T1.ARCHIVE_CLASS_NODE_TYPE,\n");
		sbSql.append("         T1.IS_VISIBLE,\n");
		sbSql.append("         CTN.TAB_ORDER AS NODE_SORT_NUMBER,\n");
		sbSql.append("         T1.RELAT_WORKFLOW,\n");
		sbSql.append("         T1.TABLE_LAYER,\n");
		sbSql.append("         T1.ARCHIVE_CLASS_NODE_PATH || '/' || CT.ID AS ARCHIVE_CLASS_NODE_PATH,\n");
		sbSql.append("         T1.REMARK,\n");
		sbSql.append("         T1.ORG_ID,\n");
		sbSql.append("         T1.DEPT_ID,\n");
		sbSql.append("         T1.SYS_ID,\n");
		sbSql.append("         T1.SECRET_LEVEL,\n");
		sbSql.append("         T1.SYS_APPLICATION_ID,\n");
		sbSql.append("         T1.CREATED_BY,\n");
		sbSql.append("         T1.CREATION_DATE,\n");
		sbSql.append("         T1.LAST_UPDATED_BY,\n");
		sbSql.append("         T1.LAST_UPDATE_DATE,\n");
		sbSql.append("         T1.LAST_UPDATE_IP,\n");
		sbSql.append("         T1.VERSION,\n");
		sbSql.append("         T1.ATTRIBUTE_01,\n");
		sbSql.append("         T1.ATTRIBUTE_02,\n");
		sbSql.append("         T1.ATTRIBUTE_03,\n");
		sbSql.append("         T1.ATTRIBUTE_04,\n");
		sbSql.append("         T1.ATTRIBUTE_05,\n");
		sbSql.append("         T1.ATTRIBUTE_06,\n");
		sbSql.append("         T1.ATTRIBUTE_07,\n");
		sbSql.append("         T1.ATTRIBUTE_08,\n");
		sbSql.append("         T1.ATTRIBUTE_09,\n");
		sbSql.append("         T1.ATTRIBUTE_10,\n");
		sbSql.append("         T1.ATTRIBUTE_11,\n");
		sbSql.append("         T1.ATTRIBUTE_12,\n");
		sbSql.append("         T1.ATTRIBUTE_13,\n");
		sbSql.append("         T1.ATTRIBUTE_14,\n");
		sbSql.append("         T1.ATTRIBUTE_15,\n");
		sbSql.append("         CTN.TAB_ORDER,\n");
		sbSql.append("         (SELECT COUNT(*)\n");
		sbSql.append("            FROM LANTOP_FIELD_RELATE_SET LFRS\n");
		sbSql.append("           WHERE LFRS.TGT_TABLE_ID = CT.ID) AS MAP_SET_TGT_COUNT,\n");
		sbSql.append("         (SELECT COUNT(*)\n");
		sbSql.append("            FROM LANTOP_FIELD_RELATE_SET LFRS\n");
		sbSql.append("           WHERE LFRS.TGT_TABLE_NAME = 'LANTOP_PUB_FIELD'\n");
		sbSql.append("             AND LFRS.SOURCE_TABLE_ID = CT.ID) AS MAP_SET_TGT_PUB_COUNT,\n");
		sbSql.append("         CT.TABLE_IS_TRUE_TABLE AS IS_TRUE_TABLE\n");
		sbSql.append("    FROM T1\n");
		sbSql.append("   INNER JOIN CBB_TAB_NODE CTN\n");
		sbSql.append("      ON T1.ID = CTN.NODE_ID\n");
		sbSql.append("   INNER JOIN CBB_TABLES CT\n");
		sbSql.append("      ON CTN.TABLE_ID = CT.ID\n");
		sbSql.append("\n");
		sbSql.append("  )\n");
		sbSql.append("SELECT T2.*\n");
		sbSql.append("  FROM T2\n");
		sbSql.append(" WHERE ARCHIVE_CLASS_NODE_CODE = 'LANTOP_PUB_FIELD'\n");
		sbSql.append(" ORDER BY T2.TAB_ORDER");
		return this.jdbcTemplate.queryForList(sbSql.toString());
	}

	/**
	 * 获取treePath
	 */
	@Override
	public String getTreePath(String nodeId) throws Exception{
		String treePath = "";
		// 保存 treePath
		StringBuffer sb = new StringBuffer();
		sb.append("select t.id as id from LANTOP_ARCHIVE_CLASS t start with t.id='" + nodeId + "' connect by prior t.parent_id=t.id");
		List<Map<String, Object>> list1 = jdbcTemplate.queryForList(sb.toString());
		if (null != list1 && list1.size() > 0) {
			String id = list1.get(list1.size() - 1).get("id") + "";
			StringBuffer sb2 = new StringBuffer();
			/**
			 * 朱锋 2014-11-12 修改 描述：因为SQL里的数据加了换行符，所以查不出TREEPATH
			 */
			sb2.append("select sys_connect_by_path(t.id, '/') as path from LANTOP_ARCHIVE_CLASS t \n");
			sb2.append(" where  t.id='" + nodeId + "'connect by prior t.id =  t.parent_id start with t.id='" + id + "'");
			/**
			 * 朱锋 2014-11-12修改完毕
			 */
			List<Map<String, Object>> list2 = this.jdbcTemplate.queryForList(sb2.toString());
			if (null != list2 && list2.size() > 0) {
				StringBuilder path = new StringBuilder();
				path.append(list2.get(0).get("path") + "");
				treePath = path.deleteCharAt(0).toString();
			}
		}
		return treePath;
	}

	/**
	 * 判断模板文件夹下是否有数据节点
	 * 
	 * @param id
	 * @return
	 */
	@Override
	public int getCountByClassFloder(String id) throws Exception {
		int count = 0;
		StringBuffer sb = new StringBuffer();
		sb.append("select count(*) as count from lantop_archive_tab_class_v t where t.PARENT_ID='" + id + "'");
		Map<String, Object> map = this.jdbcTemplate.queryForMap(sb.toString());
		Object obj = map.get("count");
		count = Integer.parseInt(obj.toString());
		return count;
	}

	/**
	 * 操作数据,格式化后返回前台
	 * 
	 * @param id
	 * @return
	 */
	@Override
	public List<Map<String, Object>> getReturnAttribute(String id) throws Exception{
		StringBuffer sb = new StringBuffer();
		sb.append("select * from LANTOP_ARCHIVE_TAB_CLASS_V t where t.id ='" + id + "'");
		List<Map<String, Object>> list = this.jdbcTemplate.queryForList(sb.toString());
		return list;
	}

	// ////////////////////////////////////////

	/**
	 * 递归获取Tree当前节点下数据
	 * 
	 * @param id
	 *            当前节点
	 * @param level
	 *            需要获取的层数
	 * @return 节点集合
	 */
	@Override
	public List<TreeNode> getCommTempletInfoTreeNodes(String id, int level, String currOrg) {
		// 递归一层减一层
		--level;
		List<TreeNode> nodes = new ArrayList<TreeNode>();
		// 查询业务数据
		List<Map<String, Object>> childTreeList = this.findCommTempletInfoNodesByParentId(id, currOrg);
		// 将业务数据格式转换成esayui树形格式，此处封装了一个TreeNodeVo节点
		for (Map<String, Object> row : childTreeList) {
			String rid = (String) row.get("id");
			String nodeType = (String) row.get("ARCHIVE_CLASS_NODE_TYPE");
			String archiveType = (String) row.get("ARCHIVE_TYPE");
			String parentId = (String) row.get("PARENT_ID");
			String rname = "";
			rname = (String) row.get("NODE_NAME");
			BigDecimal childCount2 = BigDecimal.valueOf(Long.valueOf(row.get("CHILD_COUNT") + ""));
			TreeNode node = new TreeNode();
			node.setId(rid);
			node.setText(rname);
			// 根据节点类型,设置节点图标
			if (null != parentId && "ROOT".equals(parentId)) { // 根节点
				node.setIconCls("icon-im-newnorg");
			} else if (null != archiveType && "-1".equals(archiveType)) { // 档案类型
																			// -1
																			// 物理表
				node.setIconCls("icon-im-newntable");
			} else if (null != archiveType && "0".equals(archiveType)) { // 0
																			// 模版文件夹
				node.setIconCls("icon-im-newnmodulefile");
			} else if (null != archiveType && "1".equals(archiveType)) { // 1
																			// 标识文件
				node.setIconCls("icon-im-newnmodulelib");
			} else if (null != archiveType && "2".equals(archiveType)) { // 2 档案
				node.setIconCls("icon-im-newnmodulelib");
			} else if (null != archiveType && "3".equals(archiveType)) { // 3 专题库
				node.setIconCls("icon-im-newnmodulelib");
			} else if (null != archiveType && "4".equals(archiveType)) { // 4 多媒体
				node.setIconCls("icon-im-newnmodulelib");
			} else if (null != archiveType && "5".equals(archiveType)) { // 5
																			// 项目归档计划
				node.setIconCls("icon-im-newnmodulelib");
			} else if (null != archiveType && "6".equals(archiveType)) { // 6 管务
				node.setIconCls("icon-im-newnmodulelib");
			} else if (null != archiveType && "7".equals(archiveType)) { // 7 其他
				node.setIconCls("icon-im-newnmodulelib");
			} else if (null != archiveType && "8".equals(archiveType)) { // 8 资料
				node.setIconCls("icon-im-newnmodulelib");
			} else if (null != archiveType && "9".equals(archiveType)) { // 9 图书
				node.setIconCls("icon-im-newnmodulelib");
			} else if (null != archiveType && "10".equals(archiveType)) { // 10 其他
				node.setIconCls("icon-im-newnmodulelib");
			} else if (null != nodeType && "1".equals(nodeType)) { // 节点类型 1 文件夹
				node.setIconCls("icon-im-newnmodulefile");
			} else if (null != nodeType && "2".equals(nodeType)) { // 数据节点
				node.setIconCls("icon-im-newndatanode");
			} else if (null != archiveType && "TECH".equals(archiveType)) { // 10
				node.setIconCls("icon-im-newnmodulelib");
			}
			node.setAttributes(row);
			node.set_parentId((String) row.get("PARENT_ID"));
			// 根据档案档案类型构造子节点
			if ("1".equals(archiveType)) {
				// 拼接个人文件和部门文件两个固定节点
				List<Map<String, Object>> list = this.getLantopArchivebyPrentId(rid);
				if (list.size() > 0) {
					for (Map<String, Object> map : list) {
						TreeNode childNode = new TreeNode();
						childNode.setId((String) map.get("ID"));
						childNode.setIconCls("icon-im-newndatanode");
						childNode.setText((String) map.get("ARCHIVE_CLASS_NODE_NAME"));
						childNode.setAttributes(map);
						childNode.set_parentId((String) map.get("PARENT_ID"));
						// 在个人和部门两个节点下放文件类型下的库表
						List<Map<String, Object>> tabInfoList = this.getTableInfoByArchiveId(rid);
						if (tabInfoList.size() > 0) {
							for (Map<String, Object> tabInfoMap : tabInfoList) {
								TreeNode tabNode = new TreeNode();
								tabNode.setId((String) tabInfoMap.get("ID"));
								tabNode.setIconCls("icon-im-newntable");
								tabNode.setText((String) tabInfoMap.get("TABLE_TITLE"));
								tabNode.set_parentId((String) map.get("ID"));
								Map<String, Object> attrMap = new HashMap<String, Object>();
								attrMap.put("ARCHIVE_TYPE", "-1");
								attrMap.put("SYS_ID", (String) tabInfoMap.get("SYS_ID"));
								attrMap.put("FILE_FLAG", (String) map.get("ARCHIVE_CLASS_NODE_CODE"));
								attrMap.put("TABLE_NAME", (String) tabInfoMap.get("TABLE_NAME"));
								attrMap.put("ID", (String) tabInfoMap.get("ID"));
								attrMap.put("PID", (String) map.get("ID"));
								tabNode.setAttributes(attrMap);
								// 将库表节点挂在个人文件、部门文件节点下
								childNode.getChildren().add(tabNode);
							}
						}
						// 将节点挂在文件节点下
						node.getChildren().add(childNode);
						childNode.setState("closed");
					}
				}
				node.setState("closed");
			} else {
				if (null != childCount2 && childCount2.longValue() > 0) {
					if (level > 0) {
						List<TreeNode> cnodes = this.getCommTempletInfoTreeNodes(rid, level, currOrg);
						// add by gl 2016-05-26 start 去除虚拟表节点
						for (int i = 0; i < cnodes.size(); i++) {
							TreeNode treeNode = cnodes.get(i);
							Map<String, Object> nodeattr = (Map<String, Object>) treeNode.getAttributes();
							if ("10".equals(nodeattr.get("ARCHIVE_TYPE"))) {
								cnodes.remove(i);
							}
						}
						// add by gl 2016-05-26 end 去除虚拟表节点
						node.getChildren().addAll(cnodes);
						node.setState("open");
					} else {
						node.setState("closed");
					}
				} else {
					node.setState("open");
				}
			}
			nodes.add(node);
		}
		return nodes;
	}

	/**
	 * 获取子节点
	 * 
	 * @param parentId
	 * @return
	 */
	public List<Map<String, Object>> findCommTempletInfoNodesByParentId(String parentId, String currOrg) {
		Assert.notNull(parentId, "参数不能为空！");
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT T.*,\n");
		sb.append("       (SELECT COUNT(*)\n");
		sb.append("          FROM LANTOP_ARCHIVE_CLASS_TABLE_V F\n");
		sb.append("         WHERE F.PID = '" + parentId + "' AND F.ORG_ID = '" + currOrg + "'" + " \n");
		sb.append("        ) CHILD_COUNT\n");
		sb.append("  FROM LANTOP_ARCHIVE_CLASS_TABLE_V T\n");
		sb.append(" WHERE T.PID = '" + parentId + "'  AND T.ORG_ID = '" + currOrg + "'\n");
		sb.append("CONNECT BY PRIOR T.ID = T.PID\n");
		sb.append(" START WITH T.PID = '" + parentId + "'");
		sb.append(" ORDER BY T.NODE_SORT_NUMBER ASC,T.CREATION_DATE ASC");
		return this.jdbcTemplate.queryForList(sb.toString());
	}

	/**
	 * 参考模板复制库结构字段加载树时递归获取Tree当前节点下数据
	 * 
	 * @param id
	 *            当前节点
	 * @param level
	 *            需要获取的层数
	 * @return 节点集合
	 */
	public List<TreeNode> recurFindTreeNodesByParentIdInCommTemplet(String id, int level, String orgId) {
		// 递归一层减一层
		--level;
		List<TreeNode> nodes = new ArrayList<TreeNode>();
		// 查询业务数据
		List<Map<String, Object>> childTreeList = findCommTempletInfoNodesByParentIdInCommTemplet(id);
		// 将业务数据格式转换成esayui树形格式
		for (Map<String, Object> row : childTreeList) {
			String rid = (String) row.get("ID");
			String rname = (String) row.get("ARCHIVE_CLASS_NODE_NAME");
			String nodeType = (String) row.get("ARCHIVE_CLASS_NODE_TYPE");
			String archiveType = (String) row.get("ARCHIVE_TYPE");
			String parentId = (String) row.get("PARENT_ID");
			TreeNode node = new TreeNode();
			node.setId(rid);
			node.setText(rname);
			// 根据节点类型,设置节点图标
			if (archiveType == null || "".equals(archiveType)) { // 手动添加的根节点
				node.setIconCls("icon-im-newrefermodule");
			} else // 根据节点类型,设置节点图标
			if (null != parentId && "ROOT".equals(parentId)) { // 根节点
				node.setIconCls("icon-im-newnorg");
			} else if (null != archiveType && "-1".equals(archiveType)) { // 档案类型
																			// -1
																			// 物理表
				node.setIconCls("icon-im-newntable");
			} else if (null != archiveType && "0".equals(archiveType)) { // 0
																			// 模版文件夹
				node.setIconCls("icon-im-newnmodulefile");
			} else if (null != archiveType && "1".equals(archiveType)) { // 1
																			// 标识文件
				node.setIconCls("icon-im-newnmodulelib");
			} else if (null != archiveType && "2".equals(archiveType)) { // 2 档案
				node.setIconCls("icon-im-newnmodulelib");
			} else if (null != archiveType && "3".equals(archiveType)) { // 3 专题库
				node.setIconCls("icon-im-newnmodulelib");
			} else if (null != archiveType && "4".equals(archiveType)) { // 4 多媒体
				node.setIconCls("icon-im-newnmodulelib");
			} else if (null != archiveType && "5".equals(archiveType)) { // 5
																			// 项目归档计划
				node.setIconCls("icon-im-newnmodulelib");
			} else if (null != archiveType && "6".equals(archiveType)) { // 6 管务
				node.setIconCls("icon-im-newnmodulelib");
			} else if (null != archiveType && "7".equals(archiveType)) { // 7 其他
				node.setIconCls("icon-im-newnmodulelib");
			} else if (null != archiveType && "8".equals(archiveType)) { // 8 资料
				node.setIconCls("icon-im-newnmodulelib");
			} else if (null != archiveType && "9".equals(archiveType)) { // 9 图书
				node.setIconCls("icon-im-newnmodulelib");
			} else if (null != archiveType && "10".equals(archiveType)) { // 10虚拟表
				node.setIconCls("icon-im-newnmodulelib");
			} else if (null != nodeType && "1".equals(nodeType)) { // 节点类型 1 文件夹
				node.setIconCls("icon-im-newnmodulefile");
			} else if (null != nodeType && "2".equals(nodeType)) { // 数据节点
				node.setIconCls("icon-im-newntable");
			} else if (null != archiveType && "TECH".equals(archiveType)) { // 10
				node.setIconCls("icon-im-newnmodulelib");
			}
			node.setAttributes(row);
			node.set_parentId((String) row.get("PARENT_ID"));

			BigDecimal childCount2 = BigDecimal.valueOf(Long.valueOf(row.get("CHILD_COUNT") + ""));
			if (null != childCount2 && childCount2.longValue() > 0) {
				if (level > 0) {
					List<TreeNode> cnodes = recurFindTreeNodesByParentId(rid, level, orgId);
					node.getChildren().addAll(cnodes);
					node.setState("open");
				} else {
					node.setState("closed");
				}
			} else {
				node.setState("open");
			}
			nodes.add(node);
		}
		return nodes;
	}

	/**
	 * 参考模板复制库结构字段加载树时获取子节点
	 * 
	 * @param parentId
	 * @return
	 */
	public List<Map<String, Object>> findCommTempletInfoNodesByParentIdInCommTemplet(String parentId) {
		Assert.notNull(parentId, "参数不能为空！");
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT *\n");
		sb.append(" FROM (SELECT (SELECT COUNT(*)\n");
		sb.append("  FROM LANTOP_ARCHIVE_TAB_CLASS_V D2\n");
		sb.append("  WHERE D2.PARENT_ID = D1.ID) CHILD_COUNT,\n");
		sb.append("D1.ID,D1.ARCHIVE_CLASS_NODE_CODE,D1.ARCHIVE_CLASS_NODE_NAME,D1.TABLE_NAME,D1.PARENT_ID,\n");
		sb.append("D1.ARCHIVE_TYPE,D1.ARCHIVE_CLASS_NODE_TYPE,D1.IS_VISIBLE, D1.NODE_SORT_NUMBER,D1.RELAT_WORKFLOW,\n");
		sb.append("D1.TABLE_LAYER,D1.ARCHIVE_CLASS_NODE_PATH,D1.REMARK,D1.ORG_ID, D1.DEPT_ID,\n");
		sb.append("D1.SYS_ID,D1.SECRET_LEVEL,D1.SYS_APPLICATION_ID,D1.CREATED_BY,D1.CREATION_DATE,\n");
		sb.append("D1.LAST_UPDATED_BY,D1.LAST_UPDATE_DATE,D1.LAST_UPDATE_IP,D1.VERSION\n");
		sb.append("  FROM LANTOP_ARCHIVE_TAB_CLASS_V D1\n");
		sb.append("  UNION ALL\n");
		sb.append(" SELECT (SELECT COUNT(*)\n");
		sb.append(" FROM LANTOP_ARCHIVE_TAB_CLASS_V LATC\n");
		sb.append(" WHERE LATC.PARENT_ID = 'ROOT') CHILD_COUNT,\n");
		sb.append(" 'ROOT','','库结构定义分类','','-1','',	'','',0,'', 0,'', '','','','','','','',\n");
		sb.append("SYSDATE,'',SYSDATE,'',0\n");
		sb.append(" FROM DUAL) D2\n");
		sb.append(" WHERE D2.PARENT_ID = '" + parentId + "'\n");
		sb.append(" ORDER BY D2.NODE_SORT_NUMBER, D2.CREATION_DATE\n");
		return this.jdbcTemplate.queryForList(sb.toString());
	}

	/**
	 * 验证同级别的节点名称是否已经存在
	 * 
	 * @author 朱锋
	 * @param lantopArchiveClass
	 * @return boolean
	 * @throws
	 */
	@Override
	public boolean checkExtName(LantopArchiveClassT lantopArchiveClass) throws Exception {
		boolean ext = false;
		StringBuffer sbSql = new StringBuffer();
		String archiveType = lantopArchiveClass.getArchiveType();
		sbSql.append("SELECT COUNT(*)\n");
		sbSql.append("  FROM LANTOP_ARCHIVE_CLASS T\n");
		// sbSql.append(" WHERE T.PARENT_ID = '" + lantopArchiveClass.getParentId() + "'\n");
		// 虚拟表表用ORG_ID过滤，同组织下虚拟表中文和英文名都不能重复
		if ("10".equals(archiveType)) {
			sbSql.append("WHERE T.ARCHIVE_CLASS_NODE_CODE = '" + lantopArchiveClass.getArchiveClassNodeCode() + "'");
			sbSql.append(" AND T.ORG_ID = '" + lantopArchiveClass.getOrgId() + "'");
		} else {
			sbSql.append(" WHERE T.PARENT_ID = '" + lantopArchiveClass.getParentId() + "'\n");
			sbSql.append(" AND T.ARCHIVE_CLASS_NODE_NAME = '" + lantopArchiveClass.getArchiveClassNodeName() + "'");
		}
		List<Object> objList = hibernateDao.findBySQL(sbSql.toString());
		if (objList != null && objList.size() > 0) {
			int count = Integer.valueOf(String.valueOf(objList.get(0)));
			if (count > 0) {
				ext = true;
			}
		}
		return ext;
	}

	/**
	 * 删除库表后，删除字段对应设置信息
	 * 
	 * @author 朱锋
	 * @param list
	 * @throws Exception
	 *             void
	 * @throws
	 */
	@Override
	public void deleteLantopMapSet(List<Map<String, Object>> list) throws Exception {
		for (int j = 0; j < list.size(); j++) {
			String tableId = String.valueOf(list.get(j).get("ID"));// 表ID
			StringBuffer sbSql = new StringBuffer();
			sbSql.append("SELECT T.ID\n");
			sbSql.append("  FROM LANTOP_FIELD_RELATE_SET T\n");
			sbSql.append(" WHERE T.SOURCE_TABLE_ID = '" + tableId + "'\n");
			sbSql.append("    OR T.TGT_TABLE_ID = '" + tableId + "'");
			List<Object> objList = hibernateDao.findBySQL(sbSql.toString());
			for (int i = 0; i < objList.size(); i++) {
				String mapSetId = String.valueOf(objList.get(i));// 字段对应设置主表ID
				// 删除字段对应设置子表信息
				StringBuffer sbSqlItem = new StringBuffer();
				sbSqlItem.append("DELETE FROM LANTOP_FIELD_RELATE_SET_ITEM T\n");
				sbSqlItem.append(" WHERE T.LANTOP_FIELD_RELATE_SET_ID = '" + mapSetId + "'");
				jdbcAvicit.getJdbcTemplate().execute(sbSqlItem.toString());
				SysLogUtil.log("库定义结构模块", "删除字段对应设置子表记录", OpType.delete, OpResult.success, LogBaseFactory.getLogBase());
				// 删除字段对应设置主表信息
				StringBuffer sbSqlSet = new StringBuffer();
				sbSqlSet.append("DELETE FROM LANTOP_FIELD_RELATE_SET T WHERE T.ID = '" + mapSetId + "'");
				jdbcAvicit.getJdbcTemplate().execute(sbSqlSet.toString());
				SysLogUtil.log("库定义结构模块", "删除字段对应设置主表记录", OpType.delete, OpResult.success, LogBaseFactory.getLogBase());
			}
		}
	}

	/***
	 * 根据节点ID查找节点archiveType
	 */
	@Override
	public String getParentTypeById(String id) throws Exception {
		String sql = "SELECT T.ARCHIVE_TYPE FROM LANTOP_ARCHIVE_TAB_CLASS_V T WHERE T.ID = '" + id + "'";
		List<String> list = this.hibernateDao.findBySQL(sql);
		if (list != null && list.size() != 0) {
			return list.get(0);
		}
		return "";
	}

	/**
	 * 根据parentId获取档案分类信息
	 * @author 郭亮
	 * @param parentId
	 * @return
	 * @throws Exception List<Map<String,Object>>
	 */
	public List<Map<String, Object>> getLantopArchivebyPrentId(String parentId) {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		String sql = "SELECT * FROM LANTOP_ARCHIVE_CLASS T WHERE T.PARENT_ID = '" + parentId + "'";
		list = this.jdbcAvicit.queryForMap(sql, null);
		return list;
	}

	/**
	 * 根据档案分类信息ID获取表信息
	 * @author 郭亮
	 * @param archiveId
	 * @return List<Map<String,Object>>
	 */
	public List<Map<String, Object>> getTableInfoByArchiveId(String archiveId) {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		String tabIdSql = "SELECT T.TABLE_ID FROM CBB_TAB_NODE T WHERE T.NODE_ID = '" + archiveId + "'";
		List<Object> tabIdList = this.hibernateDao.findBySQL(tabIdSql);
		if (tabIdList.size() > 0) {
			String sqlParam = "";
			StringBuffer sb = new StringBuffer();
			for (Object obj : tabIdList) {
				sb.append(",'" + obj.toString() + "'");
			}

			if (!"".equals(sb)) {
				sqlParam = sb.substring(1);
			}
			String tabInfoSql = "SELECT T.* FROM CBB_TABLES T WHERE T.ID IN (" + sqlParam + ")";
			list = this.jdbcAvicit.queryForMap(tabInfoSql, null);
		}
		return list;
	}

	/***
	 * 批量修改表中文名称
	 * @param List
	 * @return
	 * @throws Exception
	 */
	@Override
	public void updateCbbTablesNameById(List<Map<String, Object>> list) throws Exception {
		for (Map<String, Object> map : list) {
			if (null == map) {
				continue;
			}
			String id = (String) map.get("id");
			String tableTitle = (String) map.get("tableTitle");
			if (null != tableTitle && !"".equals(tableTitle) && !"null".equals(tableTitle) && !"NULL".equals(tableTitle)) {
				StringBuffer sbSqlItem = new StringBuffer();
				sbSqlItem.append(" UPDATE CBB_TABLES \n ");
				sbSqlItem.append(" SET TABLE_TITLE = '" + tableTitle + "'\n");
				sbSqlItem.append(" WHERE ID = '" + id + "' ");
				jdbcAvicit.getJdbcTemplate().execute(sbSqlItem.toString());
			}
		}
	}
	
	/**
	 * 判断当前组织是否创建全宗信息
	 * @param orgId
	 * @param sysId
	 * @return
	 * @throws Exception boolean
	 * @throws
	 */
	public boolean checkCurrentFonds(String orgId, String sysId) throws Exception {
		String sql = "SELECT * FROM CBB_FONDS_DB WHERE ORG_ID = '" + orgId + "' AND SYS_ID = '" + sysId + "'";
		List<Map<String, Object>> list = this.jdbcAvicit.getJdbcTemplate().queryForList(sql);
		if (list.size() > 0) {
			return true;
		}
		return false;
	}
	
	/**
	 * 判断当前组织代码个数
	 * @param orgId
	 * @param sysId
	 * @param deptId
	 * @param nodeCode
	 * @return
	 * @throws Exception
	 */
	public int getNodeCodeNum(String orgId, String sysId,String deptId,String nodeCode) throws Exception{
		StringBuffer sbSql = new StringBuffer();
		sbSql.append("SELECT COUNT(*)\n");
		sbSql.append("  FROM LANTOP_ARCHIVE_CLASS T\n");
		sbSql.append(" WHERE T.ARCHIVE_CLASS_NODE_CODE LIKE '" + nodeCode + "%'"); 
		List<Object> objList = hibernateDao.findBySQL(sbSql.toString());
		int count = Integer.valueOf(String.valueOf(objList.get(0)));
		return count;
	}
}
